import { WECHAT, BYTEDANCE } from 'cc/env';
import { bannerwxwx } from './bannerwxwx';

export class InterstitialAd {
  private static instance = null as unknown as InterstitialAd;
  private defaultInterstitialAd = null;
  private defaultAdSuccess = true;
  public static Instance() {
    if (!InterstitialAd.instance) {
      InterstitialAd.instance = new InterstitialAd();
    }
    return InterstitialAd.instance;
  }
  constructor() { }
  public create() {
    if (this.defaultAdSuccess && this.defaultInterstitialAd) {
      return;
    }
    const that = this;
    if (WECHAT) {
      this.defaultInterstitialAd = wx.createInterstitialAd({ adUnitId: bannerwxwx.chapinggdi });
      this.defaultInterstitialAd.onLoad(() => {
        that.defaultAdSuccess = true;
      });
      this.defaultInterstitialAd.onError((err: any) => {
        that.defaultAdSuccess = false;
        console.log('interstitial ad error ', err);
      })
    } else if (BYTEDANCE) {
      this.defaultInterstitialAd = tt.createInterstitialAd({ adUnitId: 'c1e93733f6h4208ukl' });
      this.defaultInterstitialAd.onLoad(() => {
        that.defaultAdSuccess = true;
      });
      this.defaultInterstitialAd.onError((err: any) => {
        that.defaultAdSuccess = false;
        console.log('interstitial ad error ', err);
      })
    }
  }

  public show() {
    if (!this.defaultInterstitialAd) {
      return;
    }
    if (!this.defaultAdSuccess) {
      return;
    }
    if (WECHAT) {
      this.defaultInterstitialAd.show().catch((err: any) => {
        console.log('interstitial ad error ', err);
      })
    } else if (BYTEDANCE) {
      this.defaultInterstitialAd.show().catch((err: any) => {
        console.log('interstitial ad error ', err);
      })
    }
  }
  // 用户自己手动关闭
  public hide() { }
  // 无需销毁
  public destroy() { }
}